package com.capinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.capinfo.base.BaseController;
import com.capinfo.base.CurrentUser;
import com.capinfo.core.annotation.Log;
import com.capinfo.entity.CapService;
import com.capinfo.entity.CapServiceDocument;
import com.capinfo.service.CapServiceDocumentService;
import com.capinfo.service.CapServiceService;
import com.capinfo.util.JsonUtil;
import com.capinfo.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: sunbenjin
 * @date: 2020/2/29
 **/
@Controller
@RequestMapping(value="/service")
public class CapServiceController extends BaseController {

    @Autowired
    private CapServiceService capServiceService;
    @Autowired
    private CapServiceDocumentService capServiceDocumentService;

    @GetMapping(value = "show")
    @RequiresPermissions("service:show")
    public String show(Model model) {
        return "/service/service-list";
    }
    @ApiOperation(value = "/showServiceList", httpMethod = "GET", notes = "展示机构列表")
    @GetMapping(value = "showServiceList")
    @ResponseBody
    public ReType showService(Model model, CapService capService, String page, String limit) {
        if(StringUtils.isEmpty(page)){
            page = "0";
        }
        if(StringUtils.isEmpty(limit)){
            limit = "10";
        }
        return capServiceService.show(capService, Integer.valueOf(page), Integer.valueOf(limit));
    }
    @GetMapping(value = "addService")
    @RequiresPermissions("service:show")
    public String addService(Model model, HttpServletRequest request, CapService capService) {
        String tmpId = (UUID.randomUUID().toString()).replace("-","");
        capService.setDocTmpId(tmpId);
        model.addAttribute("capService",capService);
        return "/service/service-form";
    }
    @GetMapping(value = "showService")
    @RequiresPermissions("service:show")
    public String showService(Model model, HttpServletRequest request) {
       String id = request.getParameter("id");
       CapService capService = capServiceService.selectByPrimaryKey(id);

       String detail = request.getParameter("detail");
       model.addAttribute("detail",detail);
        model.addAttribute("capService",capService);
        return "/service/service-form";
    }

    @ApiOperation(value="addService",httpMethod = "POST",notes="添加或更新业务类型名称")
    @PostMapping("addService")
    @ResponseBody
    public JsonUtil addService(CapService capService){
        JsonUtil j = new JsonUtil();
        CurrentUser user = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
        try {
            if(StringUtils.isEmpty(capService.getId())){
                capService.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                capService.setCreateBy(user.getId());
                capService.setCreateDate(new Date());
                capServiceService.insertSelective(capService);

            }else{
                capService.setUpdateBy(user.getId());
                capService.setUpdateDate(new Date());
                capServiceService.updateByPrimaryKeySelective(capService);
            }
            capServiceService.batchUpdateDocument(capService);


            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }

    @ApiOperation(value="get",httpMethod = "GET",notes="根据id得业务分类实例")
    @GetMapping("get")
    @ResponseBody
    public JsonUtil get(String id){
        JsonUtil j = new JsonUtil();
        try {
            CapService capService = capServiceService.selectByPrimaryKey(id);
            JSONObject jsonObject = new JSONObject();
            if(capService==null){
               capService = new CapService();
               capService.setName("");
            }
            jsonObject.put("data",capService);
            j.setJsonObj(jsonObject);
            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }


    @Log(desc = "删除", type = Log.LOG_TYPE.DEL)
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("dict:del")
    public JsonUtil del(String id) {
//        CapServiceKind capService = capServiceService.selectByPrimaryKey(id);
        //逻辑删除
        return capServiceService.delById(id,true);
    }

    @ApiOperation(value="getServiceList",httpMethod = "GET",notes="根据业务分类id得业务名称")
    @GetMapping("getServiceList")
    @ResponseBody
    public JsonUtil getServiceList(String serviceKindId){
        JsonUtil j = new JsonUtil();
        try {
            if(StringUtils.isNotEmpty(serviceKindId)){
                CapService capService = new CapService();
                capService.setServiceKindId(serviceKindId);
                List<CapService> list = capServiceService.selectListByPage(capService);

                if(!CollectionUtils.isEmpty(list)){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("data",list);
                    j.setJsonObj(jsonObject);
                    j.setMsg("获取业务列表成功");
                }else{
                    j.setMsg("无业务列表");
                    j.setFlag(Boolean.FALSE);
                }
            }else{
                j.setFlag(Boolean.FALSE);
                j.setMsg("获取失败");
            }

        } catch (Exception e) {
            j.setMsg("获取失败");
            j.setFlag(Boolean.FALSE);

            e.printStackTrace();
        }
        return j;
    }

}
